package com.lhy.shiro;

import com.lhy.realm.MyRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @Author：罗昊宇
 * @Package：com.lhy.shiro
 * @Project：shiro-framwork
 * @name：Shiro02
 * @Date：2024/5/8 0008  19:28
 * @Filename：Shiro02
 */
public class Shiro02 {
    public static void main(String[] args) {
        //获取securityManage对象
        DefaultSecurityManager securityManager =new DefaultSecurityManager();
        //创建realm对象
        MyRealm realm = new MyRealm();
        //创建加密对象
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");//指定加密方式
        credentialsMatcher.setHashIterations(5);//指定加密次数
        //设置securityManage的realm
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        //认证方法
        UsernamePasswordToken token =new UsernamePasswordToken("lhy","123");
        try {
            subject.login(token);
            System.out.println("登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("登录失败");
        }
        boolean permitted = subject.isPermitted("user:query");
        System.out.println(permitted);
    }
}
